unit PartTimeSettingForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, DBModule, Vcl.DBCtrls, Vcl.StdCtrls,
  Vcl.ComCtrls, UserUtils, IB;

type
  TfmPartTimeSetting = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    dbcbEmployee: TDBLookupComboBox;
    cbSource: TComboBox;
    cbDestination: TComboBox;
    txtPercentage: TEdit;
    UpDown1: TUpDown;
    txtAmount: TEdit;
    mmRemark: TMemo;
    btnClose: TButton;
    btnSave: TButton;
    btnDelete: TButton;
    procedure dbcbEmployeeDropDown(Sender: TObject);
    procedure txtAmountKeyPress(Sender: TObject; var Key: Char);
    procedure dbcbEmployeeCloseUp(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }
    Operation: TOperation;
    procedure FillDetails;
  public
    { Public declarations }
  end;

var
  fmPartTimeSetting: TfmPartTimeSetting;

implementation

{$R *.dfm}

procedure TfmPartTimeSetting.btnCloseClick(Sender: TObject);
begin
  FreeAndNil(fmPartTimeSetting);
end;

procedure TfmPartTimeSetting.btnSaveClick(Sender: TObject);
begin
  with DataModule1.qrySQL do
  begin
    SQL.Clear;
    Params.Clear;
    try
      try
        case Operation of
          opNew:
            SQL.Add('insert into PARTTIME_SETTING (EMPLOYEE_ID, SOURCE, DESTINATION, ' +
                    'PERCENTAGE, AMOUNT, REMARK, CREATE_DATE, UPDATE_DATE) ' +
                    'values (:EmployeeID, :Source, :Destination, :Percentage, ' +
                    ':Amount, :Remark, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)');
          opEdit:
            SQL.Add('update PARTTIME_SETTING set SOURCE=:Source, DESTINATION=:Destination, ' +
                    'PERCENTAGE=:Percentage, AMOUNT=:Amount, REMARK=:Remark, ' +
                    'UPDATE_DATE=CURRENT_TIMESTAMP where EMPLOYEE_ID:=:EmployeeID');
        end;
        Params.ParamByName('EmployeeID').AsInteger := VarToInt(dbcbEmployee.KeyValue);
        Params.ParamByName('Source').AsString := cbSource.Text;
        Params.ParamByName('Destination').AsString := cbDestination.Text;
        Params.ParamByName('Percentage').AsInteger := UpDown1.Position;
        Params.ParamByName('Amount').AsFloat := StrToFloat(txtAmount.Text);
        Params.ParamByName('Remark').AsString := mmRemark.Text;
        ExecSQL;
        Transaction.Commit;
      except
        on E: EIBInterBaseError do
        begin
          Transaction.Rollback;
          ShowMessage(Format('IBErrorCode=%d, SQLCode=%d, Msg=%s, StackTrace=%s', [E.IBErrorCode, E.SQLCode, E.Message, E.StackTrace]));
        end;
      end;
    finally
      with DataModule1.IBTransaction1 do
        if not Active then
          Active := True;
      dbcbEmployee.KeyValue := 0;
      FillDetails;
    end;
  end;
end;

procedure TfmPartTimeSetting.btnDeleteClick(Sender: TObject);
begin
  with DataModule1.qrySQL do
  begin
    SQL.Clear;
    Params.Clear;
    try
      try
        SQL.Add('delete from PARTTIME_SETTING where EMPLOYEE_ID=:EmployeeID');
        Params.ParamByName('EmployeeID').AsInteger := VarToInt(dbcbEmployee.KeyValue);
        ExecSQL;
        Transaction.Commit;
      except
        on E: EIBInterBaseError do
        begin
          Transaction.Rollback;
          ShowMessage(Format('IBErrorCode=%d, SQLCode=%d, Msg=%s, StackTrace=%s', [E.IBErrorCode, E.SQLCode, E.Message, E.StackTrace]));
        end;
      end;
    finally
      with DataModule1.IBTransaction1 do
        if not Active then
          Active := True;
      FillDetails;
    end;
  end;
end;

procedure TfmPartTimeSetting.dbcbEmployeeCloseUp(Sender: TObject);
begin
  FillDetails;
end;

procedure TfmPartTimeSetting.dbcbEmployeeDropDown(Sender: TObject);
begin
  with DataModule1.fbEmployee do
  begin
    if not Active then
      Open;
    Last;
    First;
  end;
end;

procedure TfmPartTimeSetting.txtAmountKeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    '0'..'9':;
    '.':;
    Char(VK_BACK):;
    else
      Key := #0;
  end;
end;

procedure TfmPartTimeSetting.FillDetails;
begin
  with DataModule1.fbPartTimeSetting do
  begin
    if RecNo <= 0 then
    begin
//      txtPercentage.Text := '0';
      UpDown1.Position := 0;
      txtAmount.Text := '0.00';
      cbSource.ItemIndex := -1;
      cbDestination.ItemIndex := -1;
      mmRemark.Lines.Clear;
      Operation := opNew;
    end else begin
//      txtPercentage.Text := FieldByName('PERCENTAGE').AsString;
      UpDown1.Position := FieldByName('PERCENTAGE').AsInteger;
      txtAmount.Text := FloatToStr(FieldByName('AMOUNT').AsFloat);
      cbSource.ItemIndex := cbSource.Items.IndexOf(FieldByName('SOURCE').AsString);
      cbDestination.ItemIndex := cbDestination.Items.IndexOf(FieldByName('DESTINATION').AsString);
      mmRemark.Lines.Clear;
      mmRemark.Lines.Add(FieldByName('REMARK').AsString);
      Operation := opEdit;
    end;
  end;
end;

end.
